'''
Created on 2011-3-13

@author: Fish
'''
from pyDes import *
from aboutfamily.familyMember.models import LoginId

class EnAndDecrypt(object):
    '''
    classdocs
    '''
    global key 
    
    def __init__(self):
        '''
        Constructor
        '''
        key = 'crazefish'
    
    def __str__(self):
        return "encrpyt and decrpty the password"
    
    #encrpyt Email address to be the key for password
    def encrpytEmail(self,email):
        k = des(key,CBC,"\0\0\0\0\0\0\0\0",pad=None,padmode=PAD_PKCS5)
        return k.encrypt(email)
    
    #encrpyt password by the key which is a encrpyt word generated by email address
    def encrpytPSW(self,email,password):
        email = "fish@aboutfamily.com"
        k = triple_des(self.encrpytEmail(email),CBC,"\0\0\0\0\0\0\0\0",pad=None,padmode=PAD_PKCS5)
        encrpytCode = k.encrypt(password)
        return encrpytCode

    def decrpytPSW(self,psw):
        login_record = LoginId.objects.get(password=psw)
        email = login_record.mail
        k = triple_des(self.encrpytEmail(email),CBC,"\0\0\0\0\0\0\0\0",pad=None,padmode=PAD_PKCS5)
        return k.decrypt(psw)
    